var express = require('express')
var router = express.Router()
var db = require('../../common/blog').dal_blog_type

router.get('/list', (req, res) => {
    var searchName = ''
    if (req.query.name) {
        searchName = req.query.name
    }
    db.getData(searchName, function(dataList) {
        res.render('admin/blog_type/list', { list: dataList, query: req.query })
    })
})

router.get('/add', (req, res) => {
    res.render('admin/blog_type/add')
})

router.post('/create', (req, res) => {
    req.body.id = Date.now()
    db.save(req.body, function(isOK) {
        if (isOK) {
            res.redirect('/admin/blog_type/list')
        } else {
            //错误提示
            console.log('保存错误')
        }
    })
})

router.post('/del', (req, res) => {
    db.del(req.body.id, function(isOK) {
        if (isOK) {
            res.redirect('/admin/blog_type/list')
        } else {
            //错误提示
            console.log('保存错误')
        }
    })
})

router.get('/edit/:id', (req, res) => {
    db.findById(req.params.id, function(model) {
        res.render('admin/blog_type/edit', { model: model })
    })
})

router.post('/update/:id', (req, res) => {
    var id = req.params.id
    db.updateById(id, req.body, function(isOK) {
        if (isOK) {
            res.redirect('/admin/blog_type/list')
        } else {
            //错误提示
            console.log('修改错误')
        }
    })
})
module.exports = router